local zhisuan = fk.CreateSkill{
  name = "swis__zhisuan",
  tags = {Skill.Switch}
}

local zhisuan_mark = "@swis__zhisuan"
local suits = { "log_diamond", "log_club", "log_heart", "log_spade", }

Fk:loadTranslationTable{
  ["swis__zhisuan"] = "智算",
  [":swis__zhisuan"] = "转换技，你可以将一张"
  .."①<font color='red'>♦</font>"
  .."②♣"
  .."③<font color='red'>♥</font>"
  .."④♠"
  .."牌当一张任意基本牌使用。若为【桃】且不为<font color='red'>♥</font>牌，你失去1点体力，摸一张牌。",
  [zhisuan_mark] = "智算",
}

local U = require "packages/utility/utility"

---@return string[]
local getAllBasicCardNames = function ()
  local names = {}
  table.forEach(Fk:getAllCardIds(), function (id)
    if Fk:getCardById(id).type == Card.TypeBasic then
      table.insertIfNeed(names, Fk:getCardById(id).trueName)
    end
  end)
  return names
end

zhisuan:addAcquireEffect(function (self, player, is_start)
  player.room:setPlayerMark(player, zhisuan_mark, suits[1])
end)

zhisuan:addEffect("viewas", {
  anim_type = "switch",
  pattern = ".|.|.|.|.|basic",
  interaction = function (self, player)
    local all_choices = getAllBasicCardNames()
    local choices = player:getViewAsCardNames(zhisuan.name, all_choices)
    return UI.CardNameBox { all_choices = all_choices, choices = choices }
  end,
  card_filter = function (self, player, to_select, selected)
    if #selected ~= 0 then return false end
    local card = Fk:getCardById(to_select)
    local suit = player:getMark(zhisuan_mark)
    return card:getSuitString(true) == suit
  end,
  view_as = function (self, player, cards)
    local name = self.interaction.data
    if not name or #cards ~= 1 then return end
    local card = Fk:cloneCard(name)
    card.skillName = zhisuan.name
    card:addSubcards(cards)
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    local index = table.indexOf(suits, player:getMark(zhisuan_mark))
    index = (index + 1) % 4
    room:setPlayerMark(player, zhisuan_mark, suits[index])
  end,
  after_use = function (self, player, use)
    local room = player.room
    local card = use.card
    if card.name == "peach" and card.suit ~= Card.Heart then
      room:loseHp(player, 1, zhisuan.name)
      if player:isAlive() then
        room:drawCards(player, 1, zhisuan.name)
      end
    end
  end,
  enabled_at_response = function (self, player, response)
    return not response
  end,
})

return zhisuan